#include "MyInteractorStyleTrackballCameraEventObserver.h"
#include "Orthodontic.h"

void MyInteractorStyleTrackballCameraEventObserver::Execute(vtkObject* caller, unsigned long eventId, void* callData)
{
    MyVTKWidget* qvtkWidget = reinterpret_cast<MyVTKWidget*>(m_qvtk);
	Orthodontic* m = reinterpret_cast<Orthodontic*>(qvtkWidget->get_m());
    MyInteractorStyleTrackballCamera* style = reinterpret_cast<MyInteractorStyleTrackballCamera*>(caller);
    if (eventId == vtkCommand::LeftButtonPressEvent)
    {
		style->OnLeftButtonDown();
        //
    }
	if (eventId == vtkCommand::LeftButtonReleaseEvent)
	{
        //
		style->OnLeftButtonUp();
	}



	if (eventId == vtkCommand::MiddleButtonPressEvent)
	{
		style->OnMiddleButtonDown();
        //
	}
	if (eventId == vtkCommand::MiddleButtonReleaseEvent)
	{
        //
		style->OnMiddleButtonUp();
	}



	if (eventId == vtkCommand::RightButtonPressEvent)
	{
		style->OnRightButtonDown();
		//
	}
    if (eventId == vtkCommand::RightButtonReleaseEvent)
    {
        if (style->get_flag01()) {
            qvtkWidget->emit_0_1();
        }
        //
        style->OnRightButtonUp();
    }



    if (eventId == vtkCommand::MouseMoveEvent)
    {
        if (style->get_flag01() && style->get_static_actor() != NULL) {
            qvtkWidget->emit_0_1_opacity();
        }
        if (style->get_flag02()) {
			int points[2];
			points[0] = style->GetInteractor()->GetEventPosition()[0];
			points[1] = style->GetInteractor()->GetEventPosition()[1];
			int x_dis = points[0] - style->get_points()[0];
			int y_dis = points[1] - style->get_points()[1];
			if (qAbs(x_dis) > 1 || qAbs(y_dis) > 1) {
				if (qAbs(x_dis) > qAbs(y_dis)) {
					// x
					if (x_dis > 0) {
						// right
						if (style->get_flag02type() == 1) {
							style->Rotate(m->get_vtk1_bsfm()->get_assembly(), 1, 0, 0);
							style->Rotate(m->get_vtk2_bsfm()->get_assembly(), 1, 0, 0);
							style->Rotate(m->get_vtk3_bsfm()->get_assembly(), 1, 0, 0);
						}
						else if (style->get_flag02type() == 2) {
							style->Pan(m->get_vtk1_bsfm()->get_assembly(), 1, 0, 0);
							style->Pan(m->get_vtk2_bsfm()->get_assembly(), 1, 0, 0);
							style->Pan(m->get_vtk3_bsfm()->get_assembly(), 1, 0, 0);
						}
					}
					else {
						// left
						if (style->get_flag02type() == 1) {
							style->Rotate(m->get_vtk1_bsfm()->get_assembly(), -1, 0, 0);
							style->Rotate(m->get_vtk2_bsfm()->get_assembly(), -1, 0, 0);
							style->Rotate(m->get_vtk3_bsfm()->get_assembly(), -1, 0, 0);
						}
						else if (style->get_flag02type() == 2) {
							style->Pan(m->get_vtk1_bsfm()->get_assembly(), -1, 0, 0);
							style->Pan(m->get_vtk2_bsfm()->get_assembly(), -1, 0, 0);
							style->Pan(m->get_vtk3_bsfm()->get_assembly(), -1, 0, 0);
						}
					}
					style->set_points(points);
				}
				else {
					// y
					if (y_dis > 0) {
						// down
						if (style->get_flag02type() == 1) {
							style->Rotate(m->get_vtk1_bsfm()->get_assembly(), 0, -1, 0);
							style->Rotate(m->get_vtk2_bsfm()->get_assembly(), 0, -1, 0);
							style->Rotate(m->get_vtk3_bsfm()->get_assembly(), 0, -1, 0);
						}
						else if (style->get_flag02type() == 2) {
							style->Pan(m->get_vtk1_bsfm()->get_assembly(), 0, 1, 0);
							style->Pan(m->get_vtk2_bsfm()->get_assembly(), 0, 1, 0);
							style->Pan(m->get_vtk3_bsfm()->get_assembly(), 0, 1, 0);
						}
					}
					else {
						// up
						if (style->get_flag02type() == 1) {
							style->Rotate(m->get_vtk1_bsfm()->get_assembly(), 0, 1, 0);
							style->Rotate(m->get_vtk2_bsfm()->get_assembly(), 0, 1, 0);
							style->Rotate(m->get_vtk3_bsfm()->get_assembly(), 0, 1, 0);
						}
						else if (style->get_flag02type() == 2) {
							style->Pan(m->get_vtk1_bsfm()->get_assembly(), 0, -1, 0);
							style->Pan(m->get_vtk2_bsfm()->get_assembly(), 0, -1, 0);
							style->Pan(m->get_vtk3_bsfm()->get_assembly(), 0, -1, 0);
						}
					}
				}
				style->set_points(points);
				m->get_qvtk1()->render();
				m->get_qvtk2()->render();
				m->get_qvtk3()->render();
			}
        }
        style->OnMouseMove();
    }
}
